package com.ybkj.daijia.api.rentalcar.v1;

import com.ybkj.daijia.api.member.ErrorConstant;
import com.ybkj.daijia.api.member.transport.ResultTransport;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.server.model.Car.TransmissionCase;
import com.ybkj.daijia.server.model.CarType;
import com.ybkj.daijia.server.service.CarService;
import com.ybkj.daijia.server.service.CarTypeService;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author bo
 */
@Controller("rentalcarProcessV1Controller")
@RequestMapping(value = "rentalcar/api/rest/v1")
public class ProcessController {

    private static final Logger logger = LoggerFactory.getLogger(ProcessController.class);

    @Autowired
    private CarService carService;
    @Autowired
    private CarTypeService carTypeService;

    @RequestMapping(value = "getCar", method = RequestMethod.GET)
    public @ResponseBody
    ResultTransport getCar(String city, BigDecimal max, BigDecimal min, Long brandId,
        String[] carTypes,
        Integer sequence, Integer transmission, Integer page, Integer limit) {

        logger
            .debug("recv city:{},max:{},min:{},brandId:{},carTypes:{},sequence:{}", city, max, min,
                brandId, carTypes, sequence);

        if (StringUtils.isBlank(city) || null == page || null == limit) {
            return ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
        }

        Long[] array = null;
        if (carTypes != null) {
            List<CarType> carTypeList = carTypeService.findBycarTypeIn(carTypes);
            array = new Long[carTypeList.size()];
            if (CollectionUtils.isNotEmpty(carTypeList)) {
                for (int i = 0; i < carTypeList.size(); i++) {
                    array[i] = carTypeList.get(i).getId();
                }
            }
        }

        String sequenceStr = "DESC";//降序
        if (sequence != null && sequence == 0) {
            sequenceStr = "ASC";
        }

        TransmissionCase transmissionStr = null;
        if (transmission != null && transmission == 0) {
            transmissionStr = TransmissionCase.MT;
        } else if (transmission != null && transmission == 1) {
            transmissionStr = TransmissionCase.AT;
        } else if (transmission != null && transmission == 2) {
            //手动和自动
        } else {
            //无手动和自动
        }

        try {

            Page<Map<String, Object>> p = carService
                .findAllWithParam(city, max, min, brandId, array, sequenceStr, transmissionStr,
                    page, limit);

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
            instance.setData(p);

            return instance;

        } catch (Exception e) {
            logger.error("api v3 getCar error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
        }
    }


}
